package org.example.memora.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.example.memora.DTO.UserRedisDTO;
import org.example.memora.common.result.Result;
import org.example.memora.common.utils.UserHolder;
import org.example.memora.service.CommentLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 评论点赞控制器
 */
@RestController
@RequestMapping("/api/comment-likes")
@Tag(name = "评论点赞管理", description = "提供对评论的点赞、取消点赞、查询点赞状态等接口")
public class CommentLikeController {

    @Autowired
    private CommentLikeService commentLikeService;

    @Operation(
            summary = "评论点赞",
            description = "给指定的评论进行点赞操作。若已点赞会抛出异常或返回提示。"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "评论点赞成功"),
            @ApiResponse(responseCode = "400", description = "请求参数有误或已点赞"),
            @ApiResponse(responseCode = "401", description = "用户未登录或无权限"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping
    public Result<?> likeComment(
            @Parameter(name = "commentId", description = "被点赞评论的ID", in = ParameterIn.QUERY, required = true)
            @RequestParam String commentId
    ) {
        UserRedisDTO userRedisDTO = UserHolder.getUser();

        if(userRedisDTO==null){
            return  Result.error("获取不到UserID,请检查controller");
        }
        Long userId= userRedisDTO.getUserId();
        // 业务处理
        commentLikeService.likeComment(userId, commentId);
        return Result.success(1,"评论点赞成功");
    }

    @Operation(
            summary = "评论取消点赞",
            description = "取消对指定评论的点赞操作。"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "评论取消点赞成功"),
            @ApiResponse(responseCode = "400", description = "请求参数有误或尚未点赞"),
            @ApiResponse(responseCode = "401", description = "用户未登录或无权限"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @DeleteMapping
    public Result<?> unlikeComment(
            @Parameter(name = "commentId", description = "被取消点赞评论的ID", in = ParameterIn.QUERY, required = true)
            @RequestParam String commentId
    ) {
        UserRedisDTO userRedisDTO =UserHolder.getUser();

        if(userRedisDTO==null){
            return  Result.error("获取不到UserID,请检查controller");
        }
        Long userId= userRedisDTO.getUserId();
        // 业务处理
        commentLikeService.unlikeComment(userId, commentId);
        return Result.success(1,"取消评论点赞成功");
    }

    @Operation(
            summary = "是否已点赞评论",
            description = "判断指定用户对指定评论是否处于点赞状态。"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功：返回'已点赞'或'未点赞'"),
            @ApiResponse(responseCode = "400", description = "请求参数有误"),
            @ApiResponse(responseCode = "401", description = "用户未登录或无权限"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping
    public Result<?> hasLiked(
            @Parameter(name = "commentId", description = "评论的ID", in = ParameterIn.QUERY, required = true)
            @RequestParam String commentId
    ) {
        UserRedisDTO userRedisDTO =UserHolder.getUser();

        if(userRedisDTO==null){
            return  Result.error("获取不到UserID,请检查controller");
        }
        Long userId= userRedisDTO.getUserId();
        boolean liked = commentLikeService.hasLiked(userId, commentId);
        if(liked){
            return Result.success(1,"已点赞");
        }
        return Result.error(0,"未点赞");
    }
}

